package kr.cadis.clms.dao;

import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import kr.cadis.clms.model.Absence;
import kr.cadis.clms.model.Person;

public class PersonDao {
	
	Reader reader;
	SqlMapClient sqlMap;
	
	public PersonDao() {
		try {
			reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		}
		catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	
	public boolean insert(Person person) throws SQLException {
		sqlMap.insert("Person.insert", person);
		
		if(person.getNo() == 0) { 
			return false;
		} else {
			return true;
		}	
	}

	public boolean delete(int no) throws SQLException {
		if(sqlMap.delete("Person.delete", no) == 1) { 
			return true;
		} else {
			return false;
		}	
	}	
	
	public Person selectByPk(int no) throws SQLException {
		return (Person)sqlMap.queryForObject("Person.selectByPk", no);
	}

	public List<Person> selectAll() throws SQLException {
		List<Person> persons = sqlMap.queryForList("Person.selectAll");
		
		return persons;
	}

	public int selectAllCount() throws SQLException {
		return (Integer)sqlMap.queryForObject("Person.selectAllCount");
	}
}
